/**
 * <pre>
 * 紫光软件系统有限公司广电事业部
 * Copyright (C): 2011
 * 
 * 文件名称：
 * com.unis.iptv.dao.impl.DailyScheduleDaoImpl.java
 * 
 * 文件描述: 
 * TODO
 * 
 * Notes:
 * 修改历史(作者/日期/改动描述):
 * 杨飞宇/2013-1-2/初始化版本。
 * </pre>
 */
package com.unis.iptv.dao.impl;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;

import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.stereotype.Repository;

import com.unis.iptv.common.jdbc.BaseDao;
import com.unis.iptv.common.utils.DateUtil;
import com.unis.iptv.common.utils.GlobalUtil;
import com.unis.iptv.dao.DailyScheduleDao;
import com.unis.iptv.domain.DailySchedule;

/**
 * @date 2013-1-2
 * @author 杨飞宇
 */
@Repository
public class DailyScheduleDaoImpl extends BaseDao implements DailyScheduleDao {
	
	@Override
	public boolean saveDaillySchedule(DailySchedule dailySchedule) {
		int result = 0;
		StringBuilder sql = new StringBuilder();
		sql.append("INSERT INTO TBL_"
				+ GlobalUtil.areaAlias
				+ "_DAILY_SCHEDULE(SCHEDULE_ID, CHANNEL_ID, PROGRAM_NAME, START_TIME, END_TIME, START_DATE, DESCRIPTION, INSERT_TIME, TEM_ID, CRAWLER_NUM)VALUES( SEQ_"
				+ GlobalUtil.areaAlias
				+ "_DAILY_SCHEDULE.NEXTVAL,?,?,?,?,?,?,SYSDATE,?,?)");
		String finalSql = sql.toString();
		result = jdbcTemplate.update(
				finalSql,
				new Object[] { dailySchedule.getChannelId(),
						dailySchedule.getProgramName(),
						dailySchedule.getStartTime(),
						dailySchedule.getEndTime(),
						DateUtil.formatYearMonthDayyyMMdd(new Date()),
						dailySchedule.getDescription(), GlobalUtil.temId,
						GlobalUtil.crawlerNum });
		if (result > 0) {
			return true;
		}
		return false;
	}
	
	@Override
	public int saveDaillyScheduleList(List<DailySchedule> dailySchedules) {
		final List<DailySchedule> tempDailySchedules = dailySchedules;
		StringBuilder sql = new StringBuilder();
		sql.append("INSERT INTO TBL_"
				+ GlobalUtil.areaAlias
				+ "_DAILY_SCHEDULE(SCHEDULE_ID, CHANNEL_ID, PROGRAM_NAME, START_TIME, END_TIME, START_DATE, DESCRIPTION, INSERT_TIME, TEM_ID, CRAWLER_NUM)VALUES( SEQ_"
				+ GlobalUtil.areaAlias
				+ "_DAILY_SCHEDULE.NEXTVAL,?,?,?,?,?,?,SYSDATE,?,?)");
		String finalSql = sql.toString();
		int[] result = jdbcTemplate.batchUpdate(finalSql,
				new BatchPreparedStatementSetter() {
					
					@Override
					public void setValues(PreparedStatement preparedstatement,
							int i) throws SQLException {
						int channelId = tempDailySchedules.get(i)
								.getChannelId();
						String programName = tempDailySchedules.get(i)
								.getProgramName();
						String startTime = tempDailySchedules.get(i)
								.getStartTime();
						String endTime = tempDailySchedules.get(i).getEndTime();
						String startDate = tempDailySchedules.get(i)
								.getStartDate();
						String description = tempDailySchedules.get(i)
								.getDescription();
						int temId = GlobalUtil.temId;
						String crawlerNum = GlobalUtil.crawlerNum;
						preparedstatement.setInt(1, channelId);
						preparedstatement.setString(2, programName);
						preparedstatement.setString(3, startTime);
						preparedstatement.setString(4, endTime);
						preparedstatement.setString(5, startDate);
						preparedstatement.setString(6, description);
						preparedstatement.setInt(7, temId);
						preparedstatement.setString(8, crawlerNum);
					}
					
					@Override
					public int getBatchSize() {
						return tempDailySchedules.size();
					}
				});
		
		return result.length;
	}
}
